/*
 * $Id: $
 * 
 * Copyright (c) 2001-2008 Accentrix Company Limited. All Rights Reserved.
 * 
 * Accentrix Company Limited. ("Accentrix") retains copyright on all text, source
 * and binary code contained in this software and documentation. Accentrix grants
 * Licensee a limited license to use this software, provided that this copyright
 * notice and license appear on all copies of the software. The software source
 * code is provided for reference purposes only and may not be copied, modified 
 * or distributed.
 * 
 * THIS SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS," WITHOUT ANY WARRANTY OF
 * ANY KIND UNLESS A SEPARATE WARRANTIES IS PURCHASED FROM ACCENTRIX AND REMAINS
 * VALID.  ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ACCENTRIX SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR MODIFYING THE
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * IN NO EVENT WILL ACCENTRIX BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE
 * USE OF OR INABILITY TO USE SOFTWARE, EVEN IF ACCENTRIX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */
package com.lightsaber.trade.cloth.web.programme;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.lightsaber.trade.core.web.BaseAction;
import com.lightsaber.trade.cloth.entity.programme.Programme;
import com.lightsaber.trade.cloth.entity.programme.Stream;
import com.lightsaber.trade.cloth.service.programme.ProgrammeManager;
import com.lightsaber.trade.cloth.service.programme.StreamManager;

@Namespace("/programme")
public class StreamAction extends BaseAction {

    private static final long serialVersionUID = 161519400935008339L;
    private static final Logger LOG = LoggerFactory.getLogger(StreamAction.class);

    @Autowired
    private StreamManager streamManager;
    @Autowired
    private ProgrammeManager programmeManager;

    private String programmeId;

    private Stream stream;

    private boolean canEdit;

    @Override
    public String input() throws Exception {
        if (stream != null && StringUtils.isNotBlank(stream.getId())) {
            stream = streamManager.get(stream.getId());
        } else {
            stream = new Stream();
        }
        setCanEdit(true);
        Programme programme = programmeManager.get(programmeId);
        if (null!=programme.getStatus()) {
            setCanEdit(programme.getStatus().isCanEdit());
        }

        stream.setCode(programme.getCode());
        stream.setDuration(programme.getDuration());
        stream.setDurationType(programme.getDurationType());

        LOG.debug("### input method...");
        return INPUT;
    }

    public void validateSave() {
        // 1. in one programme do not allow two streams have same stream code
        if (streamManager.hasSameStreamCode(programmeId, stream.getStreamCode(), stream.getId())) {
            addActionError("Duplicated stream code in the programme, please enter new stream code and retry.");
        }
    }

    public String save() throws Exception {
        streamManager.save(stream, programmeId);

        addActionMessage(getText(PROCESSED_SUCCESS));

        return input();
    }

    public String getProgrammeId() {
        return programmeId;
    }

    public void setProgrammeId(final String programmeId) {
        this.programmeId = programmeId;
    }

    public Stream getStream() {
        return stream;
    }

    public void setStream(final Stream stream) {
        this.stream = stream;
    }

    public boolean isCanEdit() {
        return canEdit;
    }

    public void setCanEdit(boolean canEdit) {
        this.canEdit = canEdit;
    }

}
